package com.jie.educms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jie.commonutils.ResultEntity;
import com.jie.educms.entity.CrmBanner;
import com.jie.educms.service.CrmBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用于管后台系统banner的管理接口
 * </p>
 *
 * @author xzj
 * @since 2020-10-14
 */
//@CrossOrigin  // 解决跨域问题
@RestController
@RequestMapping("/educms/banneradmin")
public class BannerAdminController {

    private final CrmBannerService crmBannerService;

    @Autowired
    public BannerAdminController(CrmBannerService crmBannerService) {
        this.crmBannerService = crmBannerService;
    }

    /**
     * 1.分页查询banner
     * @param current  当前页
     * @param limit    一页多少条数据
     * @return         返回数据
     */
    @GetMapping("/pageBanner/{current}/{limti}")
    public ResultEntity pageBanner(
        @PathVariable("current") Long current,
        @PathVariable("limit") Long limit
    ) {
        Page<CrmBanner> bannerPage = new Page<>(current, limit);
        crmBannerService.page(bannerPage, null);
        return ResultEntity.ok().data("items", bannerPage.getRecords()).data("total", bannerPage.getTotal());
    }

    /**
     * 2.添加banner
     * @param crmBanner  要添加的banner信息
     * @return           返回是否成功信息
     */
    @PostMapping("/addBanner")
    public ResultEntity addBanner(
            @RequestBody CrmBanner crmBanner
    ) {
        crmBannerService.save(crmBanner);
        return ResultEntity.ok();
    }

    /**
     * 3.修改banner
     * @param crmBanner  要修改的banner信息
     * @return           返回是否成功的信息
     */
    @PutMapping("/update")
    public ResultEntity updateById(
            @RequestBody CrmBanner crmBanner
    ) {
        crmBannerService.updateById(crmBanner);
        return ResultEntity.ok();
    }

    /**
     * 4.通过id删除banner
     * @param id  要删除的banner的id
     * @return    返回是否成功的信息
     */
    @DeleteMapping("/remove/{id}")
    public ResultEntity removeById(
            @PathVariable("id") String id
    ) {
        crmBannerService.removeById(id);
        return ResultEntity.ok();
    }

    /**
     * 5.根据id查询banner
     * @param id  要查询的banner的id
     * @return    返回查询到的banner
     */
    @GetMapping("/get/{id}")
    public ResultEntity get(
            @PathVariable("id") String id
    ) {
        CrmBanner crmBanner = crmBannerService.getById(id);
        // 单词错了, 但是不敢改
        return ResultEntity.ok().data("itme", crmBanner);
    }

}

